﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Entity;
using UDS.Components;

namespace UDS.WebApp.SubModule
{
    public partial class FileListPage : CFPageBase
    {
        protected int CatalogID
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            phManagePanel.Visible = CanManageCF;

            // Hook event handlers
            // btnDelete.Click += new EventHandler(OnDelete_Click);
            dgFile.ItemCommand += new DataGridCommandEventHandler(OnDataGrid_ItemCommand);

            // Read parameters
            CatalogID = QSUtil.GetInt32("CatalogID", -1);
            listCatalog.SelectedCatalog = CatalogID;

            base.OnInit(e);
        }

        private void OnDataGrid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete" && e.CommandArgument != null)
            {
                int fileId = int.Parse(e.CommandArgument.ToString());
                try
                {
                    CommonFile.DeleteFiles(new int[] { fileId });
                    SiteContext.Current.AlertAndRedirect("删除成功!", Request.FilePath + "?CatalogID=" + CatalogID);
                }
                catch (Exception ex)
                {
                    SiteContext.Current.Alert("删除失败：" + ex.Message);
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }

            base.OnLoad(e);
        }

        private void BindData()
        {
            dgFile.DataSource = CommonFile.GetFilesByCatalog(CatalogID);
            dgFile.DataBind();
        }

#if false
        private void OnDelete_Click(object sender, EventArgs e)
        {
            List<int> fileIdList = new List<int>();
            foreach (DataGridItem dgi in dgFile.Items)
            {
                CheckBox cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked == true)
                {
                    string id = dgFile.DataKeys[dgi.ItemIndex].ToString();
                    fileIdList.Add(int.Parse(id));
                }
            }

            if (fileIdList.Count == 0)
            {
                SiteContext.Current.Alert("请选择要删除的文件!");
            }
            else
            {
                try
                {
                    CommonFile.DeleteFiles(fileIdList.ToArray());
                    SiteContext.Current.AlertAndRedirect("删除成功!", Request.FilePath + "?CatalogID=" + CatalogID);
                }
                catch (Exception ex)
                {
                    SiteContext.Current.Alert("删除失败：" + ex.Message);
                }
            }
        }
#endif

        protected void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgFile.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
    }
}
